<?php

namespace app\api\validate;

use think\Validate;
use app\api\model\Merchant as MerchantModel;
class Merchant extends Validate
{
    /**
     * 定义验证规则
     * 格式：'字段名'	=>	['规则1','规则2'...]
     *
     * @var array
     */	
	protected $rule = [
	    'id'=>'require|number',
	    'phone'=>'require|number',
	    'phone_code'=>'require|number',
        'code'=>'require|number',
        'category'=>'require|number',
        'repassword'=>'confirm:password',
        'password'=>'require|compare',
        'type'=>'require|in:1,2',
        'name'=>'require',
        'email'=>'email|checkEmail|require',
        'country'=>'require|number',
        'airport'=>'require|number',
        'terminal'=>'require',
        'avatar'=>'require',
//        'sub_category'=>'array',
//        'payment'=>'array',
        'feature'=>'require',
        'phone_number'=>'number',
        'telephone'=>'number',
        'address_link'=>'url',
        'end_time'=>'requireWith:start_time'

    ];
    
    /**
     * 定义错误信息
     * 格式：'字段名.规则名'	=>	'错误信息'
     *
     * @var array
     */	
    protected $message = [
        'id.require'=>'ID cannot be empty',
        'phone.require'=>'Phone cannot be empty',
        'phone_code.require'=>'Phone code cannot be empty',
        'code.require'=>'Code cannot be empty',
        'category.require'=>'Category cannot be empty',
        'type.require'=>'Type cannot be empty',
        'country.require'=>'Country cannot be empty',
        'airport.require'=>'Airport cannot be empty',
        'email.require'=>'Email cannot be empty',
        'terminal.require'=>'Terminal cannot be empty',
        'password.require'=>'Password cannot be empty',
        'id.number'=>'ID must be a number',
        'phone.number'=>'Phone must be a number',
        'phone_code.number'=>'Phone code be a number',
        'code.number'=>'Code must be a number',
        'type.in'=>'Type must be 1 or 2',
        'repassword.confirm'=>'Inconsistent passwords entered twice',
        'name.require'=>'Name cannot be empty',
        'avatar.require'=>'Avatar cannot be empty',
        'name.max'=>'Name cannot exceed 30 characters in length',
        'email.email'=>'Incorrect email format',
        'country.number'=>'Country must be a number',
        'airport.number'=>'Airport must be a number',
        'category.number'=>'Category must be a number',
        'address_link.url'=>'Please fill in the correct address link',
//        'sub_category.array'=>'sub category must be a array',
//        'payment.array'=>'sub category must be a array',
        //'feature.require'=>'feature cannot be empty',
        'phone_number.number'=>'Phone Number must be a number',
        'telephone.number'=>'Telephone must be a number',
        'end_time.requireWith'=>'End time cannot be empty',
    ];

    protected $scene=[
        'index'=>['id'],
        'login_1'=>['email','code','type'],
        'complete'=>['country','airport','terminal','category','phone_number','telephone','end_time','name','email','password','repassword','address_link'],
        'edit'=>['country','airport','terminal','category','phone_number','telephone','end_time','address_link'],
        'avatar'=>['avatar']
    ];

    protected function compare($value,$rule,$data=[])
    {
        if(isset($data['password']) && !isset($data['repassword'])){
            return 'Repassword cannot be empty';
        }

        //定义正则匹配规则
        $pattern = '/^(?=.*\d)(?=.*[a-zA-Z])[\da-zA-Z~!@#$%^&*]{8,}$/';
        //使用preg_match()函数进行匹配
        if (!preg_match($pattern, $data['password'])) {
            return 'The password must contain letters, numbers, and be at least 8 digits in length';
        }

        return true;
    }

    /**
     * 验证是否唯一
     * @param $value
     * @param $rule
     * @param array $data
     * @return bool|string
     */
    protected function checkEmail($value,$rule,$data=[])
    {
        $count=MerchantModel::where(['email'=>$value])->where('status','>=',0)->count();
        if($count){
            return 'The current email is already bound and cannot be changed';
        }else{
            return true;
        }
    }

    public function sceneLogin_1(){
        return $this->only(['email','code','type'])
            ->remove('email','checkEmail');
    }

    public function sceneLogin_2(){
        return $this->only(['email','password','type'])
            ->remove('password','compare')
            ->remove('email','checkEmail')
            ->append('password', 'require');
    }

    public function scenelogoff(){
        return $this->only(['email','code',])
            ->remove('email','checkEmail');
    }
}
